import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router'
import { WebsocketService } from '../../services/websocket.service';
import { ModalController} from '@ionic/angular';
import { LoginPage} from '../login/login.page';
import { LoginInfoService } from '../.././services/login-info.service';
import { commService } from '../../services/comm.service';
import { HttpService } from '../../services/http.service';
import { AlertController } from '@ionic/angular';
import {EventService  } from '../../services/QaService';
@Component({
  selector: 'app-mycenter',
  templateUrl: './mycenter.page.html',
  styleUrls: ['./mycenter.page.scss'],
})
export class MycenterPage implements OnInit {
  public userInfo:any
  public loginN:boolean = true;
  public veision: any;
  constructor(
    public router: Router,
    public eventService:EventService ,
    public  commService:commService,
    public  HttpService:HttpService,
    public modalController: ModalController, 
    public websocket: WebsocketService ,
    public LoginInfoService:LoginInfoService,
    public alertCtrl: AlertController,
    ){
      this.getInfo();
      this.eventService.eventEmitter.on('loginEvent',()=>{
        this.getInfo();
        console.log("---------mycenterloginEvent-------")
      })
    }
  getInfo(){
    this.userInfo = this.LoginInfoService.getInfo();
    if(!this.LoginInfoService.getToken()){
      this.loginN = false
    }else{
      this.loginN = true
    }
  }

  
  ngOnInit() {
    this.commService.getVersionInfo().subscribe(res => {
      this.veision = res;
    });
  }


  isUpdate() {
    // 1、获取当前应用的版本号
    console.log(1111)
    this.HttpService.Que_version({}).then((value: any) => {
      // console.log(this.commService.getVersionInfo())
      this.commService.getVersionInfo().subscribe(res => {
        if(value.item.appTitle !== res){
          this.showAlert(value.item.appContent);
        }
      });
      //2、请求服务器接口获取服务器的版本号

    }).catch(err => {
      console.log('getVersionNumber:' + err);
    });

  }

  async showAlert(info) {

    //3.弹窗提示用户是否更新    
    const alert = await this.alertCtrl.create({
      header: '版本升级!',
      message: info,
      buttons: [{
          text: '确认',
          handler: () => {
            //4.下载apk
            // this.downloadApp();
          }
        }
      ]
    });
    await alert.present();

  }


  //去个人信息
  tomydata() {
    this.router.navigate(['/mycenter/mydata'])
  }
  toupload(){
    if(!this.loginN){
      this.loginOut()
    }else{
      this.router.navigate(['/mycenter/uploading'],{queryParams:{}})
    }
  }

  //去实名认证
  toShiMing(){
    if(!this.loginN){
      this.loginOut()
    }else{
      this.router.navigate(['/mycenter/verified'],{queryParams:{}})
    }
  }

  //数据凭证
  tocertificate(){
    if(!this.loginN){
      this.loginOut()
    }else{
      this.router.navigate(['/mycenter/certificate'],{queryParams:{}})
    }
  }
  //扫描二维码
  toerweima(){
    this.commService.QRCode().subscribe(res =>{
      console.log(res,'+++++++')
      res = res.trim()
      let p = {
        loginName:this.userInfo.mobile,
        // loginName:'18052090503',
        uid:res
      }
      this.HttpService.loginEwm(p).then(res =>{
        console.log(res,'-----------')
      })
    })
  }
    
  //收货
  toreceive(){
    if(!this.loginN){
      this.loginOut()
    }else{
      this.router.navigate(['/mycenter/receivesite'],{queryParams:{}})
    }
  }

  cl(){
    this.router.navigate(['/mycenter/uploadImg'])
  }
  //去投诉建议
  gosuggest(){
    if(!this.loginN){
      this.loginOut()
    }else{
      this.router.navigate(['/mycenter/suggest'])
    }
  }
  //去关于我们
  goaboutmy(){
    this.router.navigate(['/mycenter/aboutmy'])
  }

  //退出登录
  outLogin(){
    this.loginOut()
    this.LoginInfoService.removeInfo()
    this.LoginInfoService.removelocal('user');
    this.LoginInfoService.removelocal('token');
    this.LoginInfoService.removelocal('firstLoginFlag2');
    // localStorage.clear();
    this.LoginInfoService.setlocal('loginusername',this.userInfo.mobile) 
    this.loginN = true
  }
  async loginOut(){
     let modal = await this.modalController.create({
        component: LoginPage,
        mode: "md",
        cssClass: "conditions",
        componentProps: {
        },
      
      });
      modal.present();
     // 模态框被关闭后回回调该方法 res 为返回值
      modal.onDidDismiss().then(res => {
        if(res.data.qrInfo=="200"){
           
        }
      })
  }
}
